/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package locusts.common;

import java.io.Serializable;

/**
 *
 * @author hamish
 */
public class Player implements Serializable {

    private final String name;
    private double x;
    private double y;

    public Player(String name, double x, double y) {
        if (name == null)
            throw new NullPointerException();
        this.name = name;
        setLocation(x, y);
    }

    public Player(String name) {
        if (name == null)
            throw new NullPointerException();
        this.name = name;
        x = Double.NaN;
        y = Double.NaN;
    }

    public void setLocation(double x, double y) {
        if (Double.isInfinite(x) || Double.isNaN(x))
            throw new IllegalArgumentException();
        if (Double.isInfinite(y) || Double.isNaN(y))
            throw new IllegalArgumentException();
        this.x = x;
        this.y = y;
    }

    public String getName() {
        return name;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public boolean isLocationSet() {
        return !(Double.isNaN(x) || Double.isNaN(y));
    }

    public double distanceTo(Player other) {
        if (other == null)
            throw new NullPointerException();
        final double dx = other.x - this.x;
        final double dy = other.y - this.y;
        return Math.sqrt((dx * dx) + (dy * dy));
    }

    public boolean equals(Player obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        if ((this.name == null) ? (obj.name != null)
                : !this.name.equals(obj.name)) return false;
        return true;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final Player other = (Player) obj;
        if ((this.name == null) ? (other.name != null)
                : !this.name.equals(other.name)) return false;
        return true;
    }

    @Override
    public int hashCode() {
        return this.name != null ? this.name.hashCode() : 0;
    }
}
